#include "TreasureMgr.h"

bool TreasureMgr::Init()
{
	mTreasureCfgMap.clear();
	InitConfig();
	return true;
}

bool TreasureMgr::UnInit()
{
	return true;
}

void TreasureMgr::InitConfig()
{
	const TreasureAdventureCfgMap *map = g_GetTreasureAdventureCfgTable()->GetTreasureAdventureCfgMap();
	if (nullptr == map)
	{
		return;
	}
	for (TreasureAdventureCfgMap::const_iterator iter = map->begin(); iter != map->end(); iter++)
	{
		const TreasureAdventureCfgInfo &info = iter->second;
		TREASURE_CFG_MAP::iterator it = mTreasureCfgMap.find(info.quality);
		if (it == mTreasureCfgMap.end())
		{
			TREASURE_CFG_VEC vec;
			vec.push_back(iter->second);
			mTreasureCfgMap.insert(make_pair(info.quality, vec));
		}
		else
		{
			TREASURE_CFG_VEC &vec = it->second;
			vec.push_back(iter->second);
		}
	}
}

const TreasureAdventureCfgInfo* TreasureMgr::RandGame(TreasureQualityEnum quality)
{
	TREASURE_CFG_MAP::iterator it = mTreasureCfgMap.find(quality);
	if (it == mTreasureCfgMap.end())
	{
		LogErrFmtPrint("[logic] TreasureMgr::RandGame..list not exist..quality = %d", quality);
		return nullptr;
	}
	TREASURE_CFG_VEC &vec = it->second;
	size_t size = vec.size();
	int32_t rand = Random(1000 + 1);
	int32_t temp = 0;
	for (size_t i = 0; i < size; ++i)
	{
		TreasureAdventureCfgInfo &info = vec.at(i);
		temp += info.qualityRand;
		if (rand <= temp)
		{
			return &info;
		}
	}
	return nullptr;
}
